package erfgame.core.entity;

import java.util.logging.Logger;


import erfgame.core.CompassDirection;
import erfgame.core.world.World;
import erfgame.core.world.WorldAwarePrototype;

public class Monster
	extends AbstractStatefulPhysicalActiveEntity 
	implements WorldAwarePrototype<Monster>, Orientable
{
	private static final long serialVersionUID = 0;
	private static final Logger log = Logger.getLogger( Monster.class.getName() );
	private Part body;
	
	private CompassDirection orientation;
	
	public Monster( long id, EntityType entityType, Part body ) {
		super( id, entityType );
		this.body = body;
	}
	
	
	public CompassDirection getOrientation() {
		return this.orientation;
	}


	public void setOrientation(CompassDirection orientation) {
		CompassDirection oldOrientation = this.orientation;
		if( oldOrientation != orientation ) {
			this.orientation = orientation;
			this.fireEntityChangeEvent(new EntityChangeEvent<CompassDirection>(EntityEventType.orientation, oldOrientation, orientation));
		}
	}


	public Part getBody() {
		return body;
	}

	@SuppressWarnings("unchecked")
	public Monster spawn(World world) {
		Part body;
		if( this.body instanceof WorldAwarePrototype ) {
			body = ((WorldAwarePrototype<Part>)this.body).spawn(world);
		} else {
			// probably going to cause problems
			body = this.body;
			if( body != null ) {
				log.warning( this.body+" does not implement WorldAwarePrototype using shared body" );
			}
		}
		Monster spawned = new Monster( world.getNextEntityId(), this.getEntityType(), body );
		spawned.setDimensions( this.getDimensions() );
		spawned.setWorld( world );
		return spawned;
	}
	
}
